package com.yuhong.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuhong.reggie.dto.SetmealDto;
import com.yuhong.reggie.entity.Category;
import com.yuhong.reggie.entity.Page;
import com.yuhong.reggie.entity.Setmeal;
import com.yuhong.reggie.entity.SetmealDish;
import com.yuhong.reggie.exception.BusinessException;
import com.yuhong.reggie.mapper.CategoryMapper;
import com.yuhong.reggie.mapper.SetMealMapper;
import com.yuhong.reggie.mapper.SetmealDishMapper;
import com.yuhong.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetMealMapper setMealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    /**
     * 套餐
     * @开启事务
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public boolean save(SetmealDto setmealDto) {
        //完善套餐
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());

        //调用数据访问新增套餐
        setMealMapper.save(setmealDto);

        //遍历套餐
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map((setmealDish->{
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setSort(0);
            return setmealDish;
        })).collect(Collectors.toList());
        //调用数据访问批量插入
        setmealDishMapper.save(setmealDishList);
        return true;

    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Setmeal> setmealList = setMealMapper.findAll(name);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        List<SetmealDto> streamDtoList = setmealList.stream().map((setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryMapper.findByid(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        })).collect(Collectors.toList());

        Page<SetmealDto> objectPage = new Page<>();
        objectPage.setRecords(streamDtoList);
        objectPage.setTotal(pageInfo.getTotal());
        return objectPage;
    }

    /**
     * 删除套餐业务
     *
     * @param ids 要删除的列表
     * @param updateUser 修改人
     * @return
     */
    @Transactional
    @Override
    public boolean deleteByIds(List<Long> ids, Long updateUser) {
        Integer integer1 = setMealMapper.conutByIds(ids);
        if (integer1>0){
            throw new BusinessException("无法删除起售套餐");
        }
        setmealDishMapper.deleteBySetmealDishIds(ids,updateUser,LocalDateTime.now());

        setMealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());
        return true;
    }
}
